<template>
	<view :class="[isDark?'dark':'', $store.state.AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar" :class="isDark?'dark':'bg-white'" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px','background-color':isDark?'#1c1c1c':'#ffffff'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					{{$t('user.sellorder.soldorder')}}
				</view>
				<view class="action">
					<!-- <text class="cuIcon-search"></text> -->
				</view>
			</view>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		<!-- <view class="text-tips margin-top text-center text-gray text-sm">
			只显示最近30条记录，请及时处理
		</view> -->
		<view class="no-data" v-if="orderList.length==0">
			<text class="cuIcon-text"></text>{{$t('common.nodata')}}
		</view>
		<view class="order-box"  v-for="(item,index) in orderList" :key="index">
			<view class="order-main">
				<view class="order-info">
					<text class="order-id">{{$t('user.sellorder.orderid') + ':' + item.id + item.created}}</text>
					<text class="order-type bg-blue" v-if="item.shopInfo">{{getType(item.shopInfo.type)}}</text>
				</view>
				<view class="order-shop" @tap="shopInfo(item.shopInfo.id)" v-if="item.shopInfo">
					{{item.shopInfo.title}}
					<view class="order-time">{{formatDate(item.created)}}</view>
				</view>
				<view class="order-shop"v-if="!item.shopInfo">
					{{$t('user.sellorder.productexpir')}}
					<view class="order-time">{{formatDate(item.created)}}</view>
				</view>
				<view class="order-btn" v-if="item.shopInfo">
					<text class="text-red">{{item.shopInfo.price + currencyName}}</text>
					<text class="text-green margin-left" @tap="toUser(item.userEmail,item.uid)">{{$t('user.sellorder.contactuser')}}</text>
					<text class="text-blue" v-if="item.shopInfo.type==1" @tap="addressInfo(item.address)">{{$t('user.sellorder.usershipaddress')}}</text>
				</view>
			</view>
		</view>
		<view class="load-more" @tap="loadMore" v-if="orderList.length>0">
			<text>{{moreText}}</text>
		</view>
		
		<view class="cu-modal" :class="modalName=='Modal'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar justify-end" :class="isDark?'dark':'bg-white'" :style="{'background-color':isDark?'#1c1c1c':'#ffffff'}">
					<view class="content" :style="{'color':isDark?'#fff':'#333'}">{{$t('user.sellorder.recipientinfo')}}</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl" :class="isDark?'dark':'bg-white'" :style="{'text-align': 'left','background-color':isDark?'#1c1c1c':'#ffffff','color':isDark?'#fff':'#333'}">
					{{$t('global.fullname')}}:{{mjname}}<br />
					{{$t('global.phone')}}:{{mjphone}}<br />
					{{$t('user.sellorder.address')}}:{{mjaddress}}<br />
					<text style="color: orangered;"><b>{{$t('user.sellorder.asapdelivery')}}</b></text>
				</view>
			</view>
		</view>
		<view class="cu-modal" :class="modalName=='Modal1'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar justify-end" :class="isDark?'dark':'bg-white'" :style="{'background-color':isDark?'#1c1c1c':'#ffffff'}">
					<view class="content" :style="{'color':isDark?'#fff':'#333'}">{{$t('user.sellorder.usercontactinfo')}}</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl" :class="isDark?'dark':'bg-white'" :style="{'background-color':isDark?'#1c1c1c':'#ffffff','color':isDark?'#fff':'#333'}">
					<text class="text-blue" @tap="ToCopy(userEmail)">{{$t('user.sellorder.useremail')}}:{{userEmail}}</text>
				</view>
			</view>
		</view>
		<!--加载遮罩-->
		<view class="loading" v-if="isLoading==0">
			<view class="loading-main">
				<image src="../../static/loading.gif"></image>
			</view>
		</view>
		<!--加载遮罩结束-->
	</view>
</template>

<script>
	import { localStorage } from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar:this.StatusBar +  this.CustomBar,
				AppStyle:this.$store.state.AppStyle,
				
				
				isLoad:0,
				token:"",
				orderList:[],
				mjname: $t('user.sellorder.unknownname'),
				mjphone: $t('user.sellorder.unknownphone'),
				mjaddress: $t('user.sellorder.unknownaddress'),
				address:"",
				userEmail:"",
				modalName: null,
				page:1,
				moreText:$t('common.loadmoretxt'),
				
				currencyName:"",
				
				isLoading:0,
			}
		},
		onPullDownRefresh(){
			var timer = setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000)
		},
		onReachBottom() {
		    //触底后执行的方法，比如无限加载之类的
			var that = this;
			if(that.isLoad==0){
				that.loadMore();
			}
		},
		onShow(){
			var that = this;
			// #ifdef APP-PLUS
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
			if(localStorage.getItem('token')){
				that.token=localStorage.getItem('token');
			}
			that.getOrderList(false);
		},
		onLoad() {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
		},
		mounted() {
			this.getset();
		},
		methods: {
			getset() {
				var that = this;
				uni.request({
					url: that.$API.SPset(),
					method: 'GET',
					dataType: "json",
					success(res) {
						that.currencyName = res.data.assetsname;
					},
					fail(error) {
						
					}
				})
			},
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			hideModal(e) {
				this.modalName = null
			},
			getType(i){
				var arr = [ this.$('global.hardgood'), , this.$t('global.softres'), this.$t('global.payread')];
				return arr[i-1];
			},
			loadMore(){
				var that = this;
				that.moreText = that.$t('global.loading');
				that.isLoad=1;
				that.getOrderList(true);
			},
			getOrderList(isPage){
				var that = this;
				var page = that.page;
				if(isPage){
					page++;
				}
				var data = {
					"limit":8,
					"page":page,
					"token":that.token
				}
				that.$Net.request({
					url: that.$API.orderSellList(),
					data:data,
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						that.isLoad=0;
						that.moreText = that.$t('common.loadmoretxt');
						if(res.data.code==1){
							var list = res.data.data;
							if(list.length>0){
								if(isPage){
									that.page++;
									that.orderList = that.orderList.concat(list);
								}else{
									that.orderList = list;
								}
							}else{
								that.moreText = that.$t('common.nomoredata');
							}
						}
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					},
					fail: function(res) {
						that.moreText = that.$t('common.loadmoretxt');
						that.isLoad=0;
						uni.showToast({
							title: that.$t('common.networkfail'),
							icon: 'none'
						})
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					}
				})
			},
			shopInfo(sid){
				uni.navigateTo({
				    url: '../shop/shopinfo?sid='+sid
				});
			},
			formatDate(datetime) {
				var datetime = new Date(parseInt(datetime * 1000));
				// 获取年月日时分秒值  slice(-2)过滤掉大于10日期前面的0
				var year = datetime.getFullYear(),
					month = ("0" + (datetime.getMonth() + 1)).slice(-2),
					date = ("0" + datetime.getDate()).slice(-2),
					hour = ("0" + datetime.getHours()).slice(-2),
					minute = ("0" + datetime.getMinutes()).slice(-2);
				//second = ("0" + date.getSeconds()).slice(-2);
				// 拼接
				var result = year + "-" + month + "-" + date + " " + hour + ":" + minute;
				// 返回
				return result;
			},
			addressInfo(text){
				var that = this;
				let parts = text.split("|");  
				that.mjname = parts[0]; 
				that.mjphone = parts[1];
				that.mjaddress = parts[2];
				that.modalName ="Modal";
			},
			toUser(text){
				var that = this;
				if(!text||text==""){
					uni.showToast({
						title: that.$t('user.sellorder.usernosetmail'),
						icon: 'none'
					})
					return false;
				}
				that.userEmail = text;
				that.modalName ="Modal1";
			},
			ToCopy(text) {
				var that = this;
				// #ifdef APP-PLUS
				uni.setClipboardData({
					data: text,
					success: () => {
						uni.showToast({ //提示
							title: that.$t('common.copysuccess')
						})
					}
				});
				// #endif
				// #ifdef H5 
				uni.showToast({ //提示
					title: that.$t('common.copysuccess')
				})
				let textarea = document.createElement("textarea");
				textarea.value = text;
				textarea.readOnly = "readOnly";
				document.body.appendChild(textarea);
				textarea.select();
				textarea.setSelectionRange(0, text.length) ;
				var result = document.execCommand("copy") 
				textarea.remove();
				// #endif
			}
		}
	}
</script>

<style>
</style>
